Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@travetto/schema
Advanced tools
This module provide a mechanisms for registering classes and field level information as well the ability to apply that information at runtime.
The registry's schema information is defined by typescript
AST and only applies to classes registered with the @Schema
decoration.
The module utilizes AST transformations to collect schema information, and facilitate the registration process without user intervention. The class can also be described using providing a:
title
- definition of the schemadescription
- detailed description of the schemaexamples
- A set of examples as JSON or YAMLThe title
will be picked up from the JSDoc
comments, and additionally all fields can be set using the @Describe
decorator.
@Schema()
class User {
name: string;
age: number;
favoriteFood?: 'pizza'|'burrito'|'salad';
}
From this schema, the registry would have the following information:
User:
fields:
-
name: name
type": string
required: true
-
name: age
type: number
required: true
-
name: favoriteFood
type: string
required: false
allowedValues: ["pizza", "burrito", "salad" ]
This schema provides a powerful base for data binding and validation at runtime. Additionally there may be types that cannot be detected, or some information that the programmer would like to override. Below are the supported field decorators:
@Field
defines a field that will be serialized, generally used in conjunction with @Schema(false)
which disables the auto registration.@Require
defines a that field should be required@Enum
defines the allowable values that a field can have@Trimmed
augments binding to remove leading and trailing whitespace from string values@Match
defines a regular expression that the field value should match@MinLength
enforces min length of a string@MaxLength
enforces max length of a string@Min
enforces min value for a date or a number@Max
enforces max value for a date or a number@Email
ensures string field matches basic email regex@Telephone
ensures string field matches basic telephone regex@Url
ensures string field matches basic url regex@Ignore
exclude from auto schema registration@Integer
ensures number passed in is only a whole number@Float
ensures number passed in allows fractional valuesAdditionally, schemas can be nested to form more complex data structures that are able to bound and validated.
Just like the class, all fields can be defined with
description
- detailed description of the schemaexamples
- A set of examples as JSON or YAMLAnd similarly, the description
will be picked up from the JSDoc
comments, and additionally all fields can be set using the @Describe
decorator.
At runtime, once a schema is registered, a programmer can utilize this structure to perform specific operations. Specifically binding and validation.
Binding is a very simple operation, as it takes in a class registered as as @Schema
and a JS object that will be the source of the binding. Given the schema
@Schema()
class Address {
street1: string;
street2: string;
}
@Schema()
class Person {
name: string;
@Integer() age: number;
address: Address;
}
A binding operation could look like
Person.from({
name: 'Test',
age: 19.999978,
address: {
street1: '1234 Fun',
street2: 'Unit 20'
}
});
and the output would be a Person
instance with the following structure
Person(
name: 'Test',
age: 20,
address: Address(
street1: '1234 Fun',
street2: 'Unit 20'
)
)
NOTE Binding will attempt to convert/coerce types as much as possible to honor the pattern of Javascript and it's dynamic nature.
Validation is very similar to binding, but instead of attempting to assign values, any mismatch or violation of the schema will result in all errors being collected and returned.
Given the same schema as above,
@Schema()
class Address {
street1: string;
street2: string;
}
@Schema()
class Person {
name: string;
@Integer() age: number;
address: Address;
}
But now with an invalid json object
const person = Person.from({
name: 'Test',
age: 'abc',
address: {
street1: '1234 Fun'
}
});
try {
await SchemaValidator.validate(person);
} catch (e) {
if (e instanceof ValidationErrors) {
... Handle errors ...
}
}
would produce an exception similar to following structure
errors:
-
path: age
kind: type
message: 'abc' is not assignable to type number
-
path: address.street2
kind: required
message: address.street2 is a required field
Integration with other modules can be supported by extensions. The dependencies are optionalExtensionDependencies
and must be installed directly if you want to use them:
The module provides high level access for Express
support, via decorators, for validating and typing request bodies.
@SchemaBody
provides the ability to convert the inbound request body into a schema bound object, and provide validation before the controller even receives the request.
class User {
name: string;
age: number;
}
...
@Post('/saveUser')
@SchemaBody(User)
async save(req: TypedBody<User>) {
const user = await this.service.update(req.body);
return { success : true };
}
...
@SchemaQuery
provides the ability to convert the inbound request query into a schema bound object, and provide validation before the controller even receives the request.
class SearchParams {
page: number = 0;
pageSize: number = 100;
}
...
@Get('/search')
@SchemaQuery(SearchParams)
async search(req: TypedQuery<SearchParams>) {
return await this.service.search(req.query);
}
...
FAQs
Data type registry for runtime validation, reflection and binding.
The npm package @travetto/schema receives a total of 41 weekly downloads. As such, @travetto/schema popularity was classified as not popular.
We found that @travetto/schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.